﻿<?php

!defined('IN_TIPASK') && exit('Access Denied');
require TIPASK_ROOT . '/lib/alipay/alipay_service.class.php';
require TIPASK_ROOT . '/lib/alipay/alipay_notify.class.php';
require TIPASK_ROOT . '/lib/yeepay/yeepay_submit.class.php';
require TIPASK_ROOT . '/lib/iconv.func.php';

class ebankmodel {

    var $db;
    var $base;

    function __construct(&$base) {
        $this->base = $base;
        $this->db = $base->db;
    }

    function get_orderstatus_by_uid($id, $uid) {
        $order = $this->db->fetch_first("SELECT * FROM " . DB_TABLEPRE . "order WHERE `id`='$id' and uid='$uid'");
        return $order;
    } 

    /* 更新order表状态 */
    function update_status($id, $status=1) {
        $this->db->query("UPDATE `" . DB_TABLEPRE . "order` set status=$status  WHERE `id` = $id");
    }

    function aliapytransfer($rechargemoney) {
        $aliapy_config = include TIPASK_ROOT . '/data/alipay.config.php';
        $tradeid = "u-" . strtolower(random(6));
        //构造要请求的参数数组
        $parameter = array(
            "service" => "create_direct_pay_by_user",
            "payment_type" => "1",
            "partner" => trim($aliapy_config['partner']),
            "_input_charset" => trim(strtolower($aliapy_config['input_charset'])),
            "seller_email" => trim($aliapy_config['seller_email']),
            "return_url" => trim($aliapy_config['return_url']),
            "notify_url" => trim($aliapy_config['notify_url']),
            "out_trade_no" => $tradeid,
            "subject" => '财富充值',
            "body" => '财富充值',
            "total_fee" => $rechargemoney,
            "paymethod" => '',
            "defaultbank" => '',
            "anti_phishing_key" => '',
            "exter_invoke_ip" => '',
            "show_url" => '',
            "extra_common_param" => '',
            "royalty_type" => '',
            "royalty_parameters" => ''
        );
        //构造即时到帐接口
        $alipayService = new AlipayService($aliapy_config);
        $html_text = $alipayService->create_direct_pay_by_user($parameter);
        echo $html_text;
    }	
    
    function yeepaytransfer($rechargemoney,$pd_FrpId) {
        //printf(iconv('GB2312','UTF-8',urldecode('%B9%BA%C2%F2+3+%B2%C6%B8%BB%B1%D2')));exit;
        $yeepay_config = include TIPASK_ROOT . '/data/yeepay.config.php';
        $tradeid = "No-" . strtotime('now');        
		$p0_Cmd = "Buy";
		$p1_MerId = trim($yeepay_config['yeepayuser']);
		$p2_Order = $tradeid;                                //商户订单号,选填. 若不为""，提交的订单号必须在自身账户交易中唯一;为""时，易宝支付会自动生成随机的商户订单号.
		$p3_Amt = $rechargemoney;                            //支付金额,必填. 单位:元，精确到分.
		$p4_Cur = "CNY";                                     //交易币种,固定值"CNY".
		$p5_Pid = "购买 ".$rechargemoney * $this->base->setting['recharge_rate']." 财富币";   //商品名称 用于支付时显示在易宝支付网关左侧的订单产品信息.
		$p6_Pcat = "充值";            //商品种类
		$p7_Pdesc = "购买虚拟财富币"; //商品描述
		$p8_Url = trim($yeepay_config['p8_Url']);            //商户接收支付成功数据的地址,支付成功后易宝支付会向该地址发送两次成功通知.
		$p9_SAF = "0";                                       //送货地址 1 为要接受 0 为不需要
		$pa_MP = "选择快答网,没错的!";       //商户扩展信息 商户可以任意填写1K 的字符串,支付成功时将原样返回.
		$pd_FrpId = $pd_FrpId;                               //银行编码 默认为""，到易宝支付网关.若不需显示易宝支付的页面，直接跳转到各银行、神州行支付、骏网一卡通等支付页面，该字段可依照附录:银行列表设置参数值.			
		$pr_NeedResponse = "1";                              //为"1": 需要应答机制;为"0": 不需要应答机制.
        
        //构造要请求的参数数组
        $parameter = array(
		"p0_Cmd" => $p0_Cmd,
		"p1_MerId" => $p1_MerId,
		"p2_Order" => $p2_Order,                 //商户订单号,选填. 若不为""，提交的订单号必须在自身账户交易中唯一;为""时，易宝支付会自动生成随机的商户订单号.
		"p3_Amt" => $p3_Amt,                     //支付金额,必填. 单位:元，精确到分.
		"p4_Cur" => $p4_Cur,                     //交易币种,固定值"CNY".
		"p5_Pid" => $p5_Pid,                     //商品名称 用于支付时显示在易宝支付网关左侧的订单产品信息.
		"p6_Pcat" => $p6_Pcat,                   //商品种类
		"p7_Pdesc" => $p7_Pdesc,                 //商品描述
		"p8_Url" => $p8_Url,                     //商户接收支付成功数据的地址,支付成功后易宝支付会向该地址发送两次成功通知.
		"p9_SAF" => $p9_SAF,                     //送货地址 1 为要接受 0 为不需要
		"pa_MP" => $pa_MP,                       //商户扩展信息 商户可以任意填写1K 的字符串,支付成功时将原样返回.
		"pd_FrpId" => $pd_FrpId,                 //银行编码 默认为""，到易宝支付网关.若不需显示易宝支付的页面，直接跳转到各银行、神州行支付、骏网一卡通等支付页面，该字段可依照附录:银行列表设置参数值.			
		"pr_NeedResponse" => $pr_NeedResponse,   //为"1": 需要应答机制;为"0": 不需要应答机制.
		"hmac" => getReqHmacString($p2_Order,$p3_Amt,$p4_Cur,$p5_Pid,$p6_Pcat,$p7_Pdesc,$p8_Url,$pa_MP,$pd_FrpId,$pr_NeedResponse)
        );
        //构造即时到帐接口
	//生成表单提交HTML文本信息
        
	$yeepayService = new YeepaySubmit();
        //正式地址
	$html_text = $yeepayService->buildForm($parameter, "https://www.yeepay.com/app-merchant-proxy/node", "post", "确认",$yeepay_config);
        //测试地址
 	//$html_text = $yeepayService->buildForm($parameter, "http://tech.yeepay.com:8080/robot/debug.action", "post", "确认",$yeepay_config);
        //printf($html_text ."ok");exit;
        echo $html_text;
    }
	
    /**
     * 针对return_url验证消息是否是支付宝发出的合法消息
     * @return 验证结果
     */
    function aliapyverifyreturn() {
        $aliapy_config = include TIPASK_ROOT . '/data/alipay.config.php';
        $alipayNotify = new AlipayNotify($aliapy_config, $this->base->get, $this->base->post);
        return $alipayNotify->verifyReturn();
    }

}

?>